package org.datascooter.db.db2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.ConnectionPoolDataSource;

import org.datascooter.impl.ContextConnector;
import org.datascooter.inface.IDataSource;
import org.datascooter.utils.DSSettings;

public class Db2Connector extends ContextConnector {

	public Db2Connector() {
	}

	public Db2Connector(IDataSource source) throws SQLException {
		super(source);
		try {
			Class.forName("com.ibm.db2.jcc.DB2Driver"); //$NON-NLS-1$
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean isSupportPagination() {
		return true;
	}

	@Override
	public ConnectionPoolDataSource createPooledDataSource() throws SQLException {
		return null;
	}

	@Override
	public synchronized Connection getConnection() throws SQLException {
		Connection conn = DriverManager.getConnection(getDataSource().getURL(), getDataSource().getUser(), getDataSource().getPassword());
		conn.setTransactionIsolation(DSSettings.getTransactionIsolation());
		conn.setHoldability(DSSettings.getResultSetHoldability());
		conn.setAutoCommit(false);
		return conn;
	}

	@Override
	public String getContextId() {
		return "db2";
	}
}
